$(function(){
	$.ajax({
		type:'POST',
		url:'/api/info',
		data:{
			id:$('#top').find('i:eq(0)').text()
		},
		dataType:'json',
		success:function(data){
			$('#top').find('img:eq(0)').attr('src',data[0].headerimg);
			$('#top').find('b:eq(0)').text(data[0].name == null?data[0].schoolId:data[0].name);
		}
	});
	var table = $('.table:eq(0)');
	$('.help').hide();
	var type = '';
	$('.checkbox:eq(0)').find('.div').on('click',function(e){
		var me = $(this);
		type = me.attr('index');
		table.hide();
		me.css('position','absolute')
				.css('top','0px')
				.css('left','-160px')
				.css('width','900px')
				.css('height','570px')
				.css('line-height','40px')
				.css('transition','all 1s');
		me.parent().css('width','800px')
				.css('transition','all 1s');
		me.siblings('div')[1].style.position = 'absolute';
		me.siblings('div')[1].style.left = '760px';
		me.siblings('div')[1].style.top = '20px';
		me.siblings('div')[1].style.width = '160px'
		me.siblings('div')[1].style.height = '60px';
		me.siblings('div')[1].style.lineHeight='60px';
		me.siblings('div')[1].style.transition='all 1s';
		me.siblings('div')[2].style.position = 'absolute';
		me.siblings('div')[2].style.left = '760px';
		me.siblings('div')[2].style.top = '120px';
		me.siblings('div')[2].style.width = '160px'
		me.siblings('div')[2].style.height = '60px';
		me.siblings('div')[2].style.lineHeight='60px';
		me.siblings('div')[2].style.transition='all 1s';
		me.siblings('div')[3].style.position = 'absolute';
		me.siblings('div')[3].style.left = '760px';
		me.siblings('div')[3].style.top = '220px';
		me.siblings('div')[3].style.width = '160px'
		me.siblings('div')[3].style.height = '60px';
		me.siblings('div')[3].style.lineHeight='60px';
		me.siblings('div')[3].style.transition='all 1s';
		if(me.attr('index')){
			showList();
			$('.help').hide();
		}else{
			table.empty();
			setTimeout(function(){
				$('.help').show()
			},1000);
		}
		me.on('transitionend',function(){
			table.show();
		})
	});
	function showList(){
		$.ajax({
			type:'POST',
			url:'/api/admin',
			data:{
				type:type
			},
			dataType:'json',
			success:function(data){
				showItem(data)
			}
		});
	}
	function showItem(data){
		table.empty();
		table.css('z-index','999');
		switch(type){
			case '1':table.append($(`<tr>
								<th>学号</th>
								<th>用户名</th>
								<th>是否是管理员</th>
								<th>删除用户</th>
							</tr>`));
				for(var i = 0;i<data.length;i++){
					//console.log(data[i])
					var $tr = $(`<tr>
									<td>`+data[i].schoolId+`</td>
									<td>`+data[i].name+`</td>
									<td class="root">`+data[i].root+`</td>
									<td class="delete">x</td>
								</tr>`);
					$tr.attr('class','tr');
					table.append($tr);
				};break;
			case '2':table.append($(`<tr>
								<th>文件Id</th>
								<th>文件名</th>
								<th>下载量</th>
								<th>评分</th>
								<th>作者</th>
								<th>删除用户</th>
							</tr>`));
				for(var i = 0;i<data.length;i++){
					//console.log(data[i])
					var $tr = $(`<tr>
									<td>`+data[i].Id+`</td>
									<td>`+data[i].name+`</td>
									<td>`+data[i].downloads+`</td>
									<td>`+data[i].stars/data[0].starnum+`</td>
									<td>`+data[i].user+`</td>
									<td class="delete">x</td>
								</tr>`);
					$tr.attr('class','tr');
					table.append($tr);
				};break;
			case '3':table.append($(`<tr>
								<th>反馈Id</th>
								<th>反馈人学号</th>
								<th>反馈类型</th>
								<th>内容</th>
								<th>反馈人信息</th>
								<th>相关网址</th>
								<th>删除反馈</th>
							</tr>`));
				for(var i = 0;i<data.length;i++){
					//console.log(data[i])
					var $tr = $(`<tr>
									<td>`+data[i].Id+`</td>
									<td>`+data[i].schoolId+`</td>
									<td>`+data[i].fetype+`</td>
									<td>`+data[i].content+`</td>
									<td>`+data[i].mes+`</td>
									<td>`+data[i].url+`</td>
									<td class="delete">x</td>
								</tr>`);
					$tr.attr('class','tr');
					table.append($tr);
				};break;
		}
		$('.delete').on('click',function(e){
			console.log(e.target.parentNode.children[0].innerHTML)
			$.ajax({
				type:'POST',
				url:'/api/delete',
				data:{
					type:type,
					id:e.target.parentNode.children[0].innerHTML
				},
				dataType:'json',
				success:function(data){
					switch(type){
						case '1':showList('1');
								alert('删除用户'+e.target.parentNode.children[0].innerHTML+'成功');
								break;
						case '2':showList('2');
								alert('删除文件'+e.target.parentNode.children[0].innerHTML+'成功');
								break;
						case '3':showList('3');
								alert('删除反馈'+e.target.parentNode.children[0].innerHTML+'成功');
								break;
					}
				}
			});
		});
	}
	$('.root').on('click',function(e){
		$.ajax({
			type:'POST',
			url:'/api/root',
			data:{
				id:e.target.parentNode.children[0].innerHTML
			},
			dataType:'json',
			success:function(data){
				showList('1');
				alert('对'+e.target.parentNode.children[0].innerHTML+'的权限修改成功!');
			}
		});
	});
	$('.up:eq(0)').on('click',function(){
		var header = $('.help').find('input:eq(0)').val();
		var content = $('.help').find('.content:eq(0)').val();
		$.ajax({
			type:'POST',
			url:'/api/addHelp',
			data:{
				header:header,
				content:content
			},
			dataType:'json',
			success:function(data){
				alert('添加帮助成功');
			}
		});
	});
});